﻿//----------------------------------------------------------------
// <copyright file="BLL_Privilege.cs" >
//    Copyright (c) Wenzy , All rights reserved.
//    author:温正宇 E-Mail:wenzy@tom.com　MyBlog:wenzy.cnblogs.com
//    All rights reserved.
// </copyright>
//----------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Text;
using MemberShipDAL.DataObject;
using MemberShipDAL.DataObject.DataResourcesTableAdapters;
using System.Data;

namespace MemberServerBLL
{
    public class BLL_Privilege
    {
        /// <summary>
        /// 获取Resources所有数据
        /// </summary>
        /// <returns></returns>
        public DataResources.wmf_PrivilegeDataTable GetAllData()
        {
            DataResources.wmf_PrivilegeDataTable dt = new DataResources.wmf_PrivilegeDataTable();
            using (wmf_PrivilegeTableAdapter da = new wmf_PrivilegeTableAdapter())
            {
                da.Fill(dt);
            }
            return dt;
        }

        /// <summary>
        /// 获取在wmf_Privilege表中是否存对应的记录
        /// </summary>
        /// <param name="optId"></param>
        /// <param name="resId"></param>
        /// <returns></returns>
        public bool GetPrivilege(int optId, string resId, DataTable dsPrivilege)
        {
            DataView dv = dsPrivilege.DefaultView;
            dv.RowFilter = "[OperationId] = " + optId + " and [ResourcesId] ='" + resId + "'";
            if (dv.Count == 1)
            {
                return true;
            }
            return false;
        }


        /// <summary>
        /// 通过资源名称和操作名称获得操作ID
        /// </summary>
        /// <param name="resourceName"></param>
        /// <param name="optionsCNName"></param>
        /// <returns></returns>
        public string GetPrivilegeId(string resourceName, string operationCNName)
        {
            wmf_ResourcesTableAdapter daResources = new wmf_ResourcesTableAdapter();
            wmf_OperationTableAdapter daOperation = new wmf_OperationTableAdapter();

            string resourcesId = daResources.GetResourceIdByCNName(resourceName).ToString();
            int operationId = (int)daOperation.GetOperationIdByCNName(operationCNName);

            string privilegeId = GetPrivilegeId(new Guid(resourcesId),operationId);
            return privilegeId;
        }

        /// <summary>
        /// 通过资源名称和操作名称获得操作ID
        /// </summary>
        /// <param name="resourceName"></param>
        /// <param name="optionsCNName"></param>
        /// <returns></returns>
        public string GetPrivilegeId(Guid resourceId, int operationId)
        {
            wmf_PrivilegeTableAdapter daPrivilege = new wmf_PrivilegeTableAdapter();
            string privilegeId = daPrivilege.GetPrivilegeIdByOptResId(operationId, resourceId).ToString();
            return privilegeId;
        }



        /// <summary>
        /// 数据列表绑定
        /// </summary>
        /// <param name="GroupId"></param>
        /// <returns></returns>
        static public DataTable PrivilegeDataBind(string GroupId)
        {
            //取出所有操作类型
            DataResources.wmf_OperationDataTable operation = new BLL_Operation().GetAllData();

            BLL_Resources resourcesBll = new BLL_Resources();
            //取出所有资源
            DataResources.wmf_ResourcesDataTable resources = resourcesBll.GetAllData();

            //根据权限组，过滤掉不属于此权限组的权限
            resources = resourcesBll.GetAllChild(resources, GroupId);

            //取出所有权限集合
            BLL_Privilege privilegeBll = new BLL_Privilege();
            DataResources.wmf_PrivilegeDataTable privilege = privilegeBll.GetAllData();

            //创建一个用于绑定的数据集
            DataTable dt = new DataTable("ResourceaAndPrivilege");

            //创建表头列
            DataColumn dcHeader = new DataColumn("资源名称");
            dt.Columns.Add(dcHeader);

            //根据operation表的数据生成列
            foreach (DataResources.wmf_OperationRow row in operation)
            {
                DataColumn dc = new DataColumn(row.OperationCNName, typeof(bool));
                dc.ExtendedProperties.Add("OperationName", row.OperationName);
                dc.ExtendedProperties.Add("OperationId", row.OperationId);
                dt.Columns.Add(dc);
            }


            DataView dvResources = resources.DefaultView;

            //根据Resources表的数据添加行
            foreach (DataRowView row in dvResources)
            {
                DataRow dr = dt.NewRow();
                dr["资源名称"] = row["ResourcesCNName"].ToString();


                foreach (DataResources.wmf_OperationRow optrow in operation)
                {
                    dr[optrow.OperationCNName] = privilegeBll.GetPrivilege(optrow.OperationId, row["ResourcesId"].ToString(), privilege);
                }
                dt.Rows.Add(dr);
            }
            return dt;
        }
    }
}
